import round, cards, game

class Engine:
    def __init__(self): pass

    def start(self, users):
        self.users = users
        self.move = 0
        self.rung = None
        self.game = None
        self.deck = cards.Deck()
        self.lastWinner = 1
        self.playGame()

    def playGame(self):
        while(True):
            moveNow = self.move
            self._distCards(moveNow)
            print("rung is: " + self.rung)
            self.game = game.Game(self.rung)
            while(not self.game.gameComplete()):        #each game starts here
                for user in self.users:     #sends all the users the game score at the start of each game
                    wins = self.game.getWins()
                    win0 = str(wins[0])
                    win1 = str(wins[1])
                    if(wins[0] < 10):
                        win0 = "0" + win0
                    if(wins[1] < 10):
                        win1 = "0" + win1
                    user[2].send(win0 + ":" + win1)
                    print(win0 + ":" + win1)
                while(True):         #each round starts here   		
                    for user in self.users:     #sends the status of the game (your move/x to move)
                        if(moveNow == user[0]):
                            movesSoFar = self.game.getRound()
                            user[2].send("your turn!!")        #sends your move signal to the player
                            cuser = user
                        else:
                            user[2].send(self.users[moveNow][1] + " to move")       #8+3=11 tell other players, who has to move right now
                    msg = cuser[2].recv(32)         #gets the move back from the user
                    for user in self.users:
                        user[2].send(str(moveNow)+msg)      #sends that move to all the users including the one who moved
                        #print "send move to: " + str(user[1])
                    print (cuser[1] + " says: " + msg)
                    ans = self.game.addMove(cuser[0], cards.str2Card(msg))
                    if(ans == -1):
                        moveNow = (moveNow+1) % 4
                    elif(ans == -2):
                        print("this can't be happening")
                    else:
                        moveNow = ans
                        break
                print("winner is: " + str(self.users[moveNow][1]))
            winner = self.game.getWinner()
            if(self.lastWinner != winner):
                self.lastWinner = winner
                self.move = (self.move+1)%4
            print("game winner is team: " + str(winner))
            for user in self.users:     #sends the winner of each game to all the players
                user[2].send(str(winner))
            self._waitForPlayerResponse()
            print("got reponse from all the player")
            
    def _waitForPlayerResponse(self):
        print("waiting for players response")
        for user in self.users:
            msg = user[2].recv(2)
            print("user " + user[1] + " sent " + msg)
            if(msg == "ok"): pass
            else: print("this could not be happeneing")
                       
    def _distCards(self, moveNow):
        self.deck.shuffle()
        print("distributing five cards")
        for user in self.users:     #sends users 5 cards each
            for n in range(0, 5):
                user[2].send(self.deck.getCard().toString())
        self.users[moveNow][2].send("s")    #21 words; asks the user to select rung
        print("asked for rung")
        resp = self.users[moveNow][2].recv(1)
        print("rung is: " + resp)
        self.rung = resp
        print("sending run to all players")
        for user in self.users:
            user[2].send(self.rung)     #1 words; tells all the user what the rung is
        print("distributing rest of the cards") 
        for i in range(0,2):        #sends user 8 more cards
            for user in self.users:
                for n in range(0, 4):
                    user[2].send(self.deck.getCard().toString())
        
        
        
        